package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.LivingEnvironmentMapper;
import com.healthcloud.entity.LivingEnvironment;
import com.healthcloud.service.LivingEnvironmentService;

/**
 * Created by rocky on 2015/11/30.
 */
@Service
public class LivingEnvironmentServiceImpl implements LivingEnvironmentService {
    @Autowired
    private LivingEnvironmentMapper livingEnvironmentMapper;

    @Override
    public ArrayList<LivingEnvironment> findByIdCard(String  idCard) {
    	LivingEnvironment livingEnvironment=new LivingEnvironment();
    	livingEnvironment.setIdCard(idCard);
        return findByEntity(livingEnvironment);
    }

    @Override
    public ArrayList<LivingEnvironment> findByEntity(LivingEnvironment livingEnvironment) {
        return livingEnvironmentMapper.findByEntityWithOutPage(livingEnvironment);
    }

    @Override
    public LivingEnvironment saveOrUpdata(LivingEnvironment livingEnvironment) {
        ArrayList<LivingEnvironment> livingEnvironments = findByIdCard(livingEnvironment.getIdCard());
        if(livingEnvironments.isEmpty()){
        	livingEnvironment.setUpdatedTime(new Date());
        	livingEnvironment.setCreatedTime(new Date());
            livingEnvironmentMapper.save(livingEnvironment);
        }else{
        	livingEnvironment.setId(livingEnvironments.get(0).getId());
        	livingEnvironment.setCreatedTime(livingEnvironments.get(0).getCreatedTime());
        	livingEnvironment.setUpdatedTime(new Date());
            livingEnvironmentMapper.update(livingEnvironment);
        }
        return livingEnvironment;
    }
}
